package com.whlilong.jxc.abstracteditor;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.part.EditorPart;

import com.whlilong.jxc.constants.IAppConstants;
import com.whlilong.jxc.manager.TextKeyManager;
import com.whlilong.jxc.service.editor.IEditorService;
import com.whlilong.jxc.sys.CacheImage;
import com.whlilong.jxc.sys.IImageKey;

public abstract class AbstractEditor extends EditorPart implements
		IEditorService {
	protected final IWorkbenchWindow window = PlatformUI.getWorkbench()
			.getActiveWorkbenchWindow();
	protected TreeViewer treeViewer;
	protected TableViewer tableViewer;
	protected ToolItem closeButton;
	protected TextKeyManager manager = TextKeyManager.getInstance();

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {
		createContent(parent);
	}

	private void createContent(Composite parent) {
		// TODO Auto-generated method stub
		final ViewForm vf = new ViewForm(parent, SWT.FLAT | SWT.BORDER);
		vf.setTopLeft(createToolbarButtons(vf));
		vf.setContent(createGroupText(vf));

	}

	protected Control createGroupText(Composite parent) {
		// TODO Auto-generated method stub
		return null;
	}

	private Control createToolbarButtons(Composite parent) {
		// TODO Auto-generated method stub
		final ToolBar toolBar = new ToolBar(parent, SWT.FLAT);
		final ToolItem newFile = new ToolItem(toolBar, SWT.NONE);
		newFile.setText("新建F2");
		newFile.setToolTipText("添加数据");
		newFile.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.newFile));
		newFile.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				create();
			}

		});
		final ToolItem deleteButton = new ToolItem(toolBar, SWT.NONE);
		deleteButton.setText("删除F3");
		deleteButton.setToolTipText("请从表格中选择一行数据来进行删除操作");
		deleteButton.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.delete));
		deleteButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				delete();
			}
		});

		final ToolItem saveButton = new ToolItem(toolBar, SWT.NONE);
		saveButton.setText("保存F6");
		saveButton.setToolTipText("点击对添加的数据进行保存");
		saveButton.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.save));
		saveButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				save();

			}
		});
		final ToolItem searchButton = new ToolItem(toolBar, SWT.NONE);
		searchButton.setText("条件查询F8");
		searchButton.setToolTipText("查询");
		searchButton.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.serch));
		searchButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				searchByCondition();
			}
		});
		final ToolItem printButton = new ToolItem(toolBar, SWT.NONE);
		printButton.setText("打印F9");
		printButton.setToolTipText("打印");
		printButton.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.print));
		printButton.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				print();
			}
		});

		final ToolItem refreshButton = new ToolItem(toolBar, SWT.NONE);
		refreshButton.setText("刷新F5");
		refreshButton.setToolTipText("刷新");
		refreshButton.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.refresh));
		refreshButton.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				refresh();
			}

		});
		closeButton = new ToolItem(toolBar, SWT.NONE);
		closeButton.setText("关闭Esc");
		closeButton.setToolTipText("退出");
		closeButton.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.exit16));
		closeButton.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {

				dispose();
			}

		});

		return toolBar;
	}

	public final void dispose() {
		try {
			manager.removeAll();
			window.getActivePage().close();
			window.openPage(IAppConstants.default_perspective, null);
			window.getActivePage().resetPerspective();
		} catch (WorkbenchException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void setFocus() {

	}

	@Override
	public final void close() {
		dispose();
	}

	protected void createColumn(String text, final ViewerSorter up,
			final ViewerSorter down, EditingSupport editingSupport) {
		final Table table = tableViewer.getTable();
		final TableViewerColumn id = new TableViewerColumn(tableViewer,
				SWT.CENTER);
		id.getColumn().setText(text);
		id.getColumn().setResizable(true);
		id.getColumn().setAlignment(SWT.LEFT);
		id.getColumn().setWidth(100);
		if (up != null && down != null)
			id.getColumn().setToolTipText("单击排序数据");
		id.getColumn().addSelectionListener(new SelectionAdapter() {
			boolean asc = true; // 记录上一次的排序方式，默认为升序

			public void widgetSelected(SelectionEvent e) {
				// asc=true则ID的升序排序器，否则用降序
				if (up == null || down == null) {
					return;
				}
				table.setSortColumn(id.getColumn());
				table.setSortDirection(SWT.UP);
				table.setSortDirection(asc ? SWT.UP : SWT.DOWN);
				tableViewer.setSorter(asc ? up : down);
				asc = !asc;// 得到下一次排序方式
			}
		});
		id.setEditingSupport(editingSupport);
	}

	protected void createColumn(String text, final ViewerSorter up,
			final ViewerSorter down, final TableViewer viewer) {
		final Table table = viewer.getTable();
		final TableViewerColumn id = new TableViewerColumn(viewer, SWT.CENTER);
		id.getColumn().setText(text);
		id.getColumn().setResizable(true);
		id.getColumn().setAlignment(SWT.CENTER);
		if (text.equals("ID"))
			id.getColumn().setWidth(35);
		else
			id.getColumn().setWidth(120);
		if (up != null && down != null)
			id.getColumn().setToolTipText("单击排序数据");
		id.getColumn().addSelectionListener(new SelectionAdapter() {
			boolean asc = true; // 记录上一次的排序方式，默认为升序

			public void widgetSelected(SelectionEvent e) {
				// asc=true则ID的升序排序器，否则用降序
				if (up == null || down == null) {
					return;
				}
				table.setSortColumn(id.getColumn());
				table.setSortDirection(SWT.UP);
				table.setSortDirection(asc ? SWT.UP : SWT.DOWN);
				viewer.setSorter(asc ? up : down);
				asc = !asc;// 得到下一次排序方式
			}
		});
	}

}
